package com.adinnet.api.childStudyToday.service.impl;

import com.adinnet.api.child.mapper.ChildMapper;
import com.adinnet.api.child.model.Child;
import com.adinnet.api.childStudyToday.model.ChildStudyToday;
import com.adinnet.api.childStudyToday.mapper.ChildStudyTodayMapper;
import com.adinnet.api.childStudyToday.model.VO.ChildStudyTodayResp;
import com.adinnet.api.childStudyToday.model.VO.ChildStudyWeekResp;
import com.adinnet.api.childStudyToday.service.ChildStudyTodayService;
import com.adinnet.api.classsCourse.mapper.ClassCourseMapper;
import com.adinnet.api.dict.mapper.DictMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 * 今日学习时间 服务实现类
 * </p>
 *
 * @author zYshuai
 * @since 2020-12-07
 */
@Service
public class ChildStudyTodayServiceImpl extends ServiceImpl<ChildStudyTodayMapper, ChildStudyToday> implements ChildStudyTodayService {

    @Autowired
    private ChildStudyTodayMapper childStudyTodayMapper;

    @Autowired
    private ClassCourseMapper classCourseMapper;

    @Autowired
    private DictMapper dictMapper;

    @Autowired
    private ChildMapper childMapper;

    @Override
    public ChildStudyTodayResp getChildStudyToday(Integer childId) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String d = dateFormat.format(new Date());
        Child child = childMapper.selectById(childId);
        Integer t=childStudyTodayMapper.getChildThisWeekVideoTimes(childId);
        ChildStudyTodayResp studyTodayResp = new ChildStudyTodayResp();
        QueryWrapper<ChildStudyToday> wrapper = new QueryWrapper();
        wrapper.eq("child_id", childId).eq("deleted",0).eq("study_date",d)
                .orderByDesc("study_date","updated_date").last("LIMIT 1");
        ChildStudyToday childStudyToday = childStudyTodayMapper.selectOne(wrapper);
        studyTodayResp.setStudyTime(childStudyToday==null?0:childStudyToday.getStudyTime());
        studyTodayResp.setSumStudyDays(child.getSumStudyDays()==null?0:child.getSumStudyDays());
        studyTodayResp.setSumStudyNum(child.getSumStudyNum()==null?0:child.getSumStudyNum());
        studyTodayResp.setThisWeekStudyVideoTime(t==null?0:t);
        studyTodayResp.setTotalCourseNum(classCourseMapper.getCourseNumByChildId(childId,null));
        studyTodayResp.setUnlockCourseNum(classCourseMapper.getCourseNumByChildId(childId,1));
        studyTodayResp.setVideoTime(dictMapper.getDictNameByCode("STUDY_TIME"));
        return studyTodayResp;
    }

    @Override
    public ChildStudyWeekResp getThisWeekVideoTime(Integer childId) {
        ChildStudyWeekResp childStudyWeekResp = new ChildStudyWeekResp();
        Integer t=childStudyTodayMapper.getChildThisWeekVideoTimes(childId);
        childStudyWeekResp.setThisWeekStudyVideoTime(t==null?0:t);
        childStudyWeekResp.setVideoTime(dictMapper.getDictNameByCode("STUDY_TIME"));
        return childStudyWeekResp;
    }
}
